package com.scalar.chiptrack.operations.dataentry.lotcorrection.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.forms.LotCorrectionForm;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.LotCorrection;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.LotCorrectionHelper;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.utils.StringUtils;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.sql.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Ganesh
 * Date: Nov 29, 2006
 * Time: 10:15:20 AM
 */
public class LotCorrectionLoadLotDetailCommand extends ActionCommand
{
     private static final String ACTION_NAME = "/lotCorrection.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        LotCorrectionForm lotCorrectionForm = ( LotCorrectionForm ) form;
        LotCorrectionHelper lotCorrectionHelper = LotCorrectionHelper.getInstance();
        String action = request.getParameter("action");

        HashMap errors = (HashMap)request.getAttribute(ERROR_MESSAGES);
        if ( errors == null )
        {
            errors = new HashMap();
            request.setAttribute(ERROR_MESSAGES, errors );
        }

        HashMap info = (HashMap)request.getAttribute(INFO_MESSAGES);
        if ( info == null )
        {
            info = new HashMap();
            request.setAttribute(INFO_MESSAGES, info );
        }

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.LOT_CORRECTION, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList lotCategoryList;
        ArrayList processTypeList;
        String processType = lotCorrectionForm.getProcessType();

        ArrayList lotCorrectionPartNumbersLableValueBeans = new ArrayList();
        ArrayList lotCorrectionLocationsLableValueBeans   = new ArrayList();
        ArrayList lotCorrectionLotNumberLabelValueBeans   = new ArrayList();

        try
        {
            processTypeList = lotCorrectionHelper.getProcessList();

            lotCorrectionPartNumbersLableValueBeans= NewProductDAO.loadPartNumbers(null, 0, processType, true , true, true, 0);

            lotCorrectionLocationsLableValueBeans= lotCorrectionHelper.getLocationList(lotCorrectionForm.getProcessStepId());

            lotCorrectionLotNumberLabelValueBeans = lotCorrectionHelper.getLotNumbersList(lotCorrectionForm.getProcessStepId(), lotCorrectionForm.getLocationId() );

            lotCorrectionForm.setLotCorrection(null);

            LotCorrection lotCorrection = DataEntryDAO.getLotInfoByLotIdprocessType( lotCorrectionForm.getLotId(), processType );
            

            if( lotCorrection.getDateIn() == null )
            {
                lotCorrection.setDateIn( StringUtils.formatDateToString( new Date(System.currentTimeMillis()), "MM/dd/yyyy"));
            }

            lotCategoryList = lotCorrectionHelper.getLotCategoryType();

            lotCorrectionForm.setLotCorrection( lotCorrection  );

            request.setAttribute ( "processList", processTypeList );
            request.setAttribute ( "partNumbersList", lotCorrectionPartNumbersLableValueBeans );
            request.setAttribute ( "locationList", lotCorrectionLocationsLableValueBeans );
            request.setAttribute ( "lotNumberList", lotCorrectionLotNumberLabelValueBeans );
            request.setAttribute ("lotCategoryList", lotCategoryList);

            request.setAttribute("action", action);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("lotCorrection");
    }
}
